# 新笔趣阁
class NewBQG:
    # 查询小说网址
    search_books_url = "https://bqg123.net/search?form=biqusoso&keyword={}"

    # 基础网站
    base_url = "https://bqg123.net"

    # 搜索书籍
    def search_books(self, base_object, novel_name):
        """
            搜索书籍
        :param base_object:   父类对象
        :param novel_name:    小说名字
        :return:
        """
        css_select = ".s_list>a"
        # 等待加载完全
        novels = base_object.wait_css_show(self.search_books_url.format(novel_name), "CSS_SELECTOR", css_select)
        novel_list = []
        for n in novels:
            info_list = str(n.text).split("：")
            try:
                l_i = info_list[1] if len(info_list) <= 2 else info_list[-1]
                author = info_list[0]
                nc_list = str(l_i).strip().split("共")
                name = nc_list[0].strip().strip("《").strip("》")
                chapter = nc_list[1].strip().strip("章")
                novel_list.append({"name": name, "chapter": chapter, "author": author, "address": self.base_url + n.attrs["href"]})
            except:
                continue

        return novel_list

    # 获取章节信息
    def get_characters(self, base_object, novel_url, start_chapter, end_chapter):
        """
            获取章节信息
        :param base_object:     父类对象
        :param novel_url:       小说地址
        :param start_chapter:   开始章节
        :param end_chapter:     结束章节
        :return:
        """
        chapters = base_object.wait_css_show(novel_url, "ID", "add_chapter_list_link", "#add_chapter_list_link>li>a")
        chapter_list = []
        for c in chapters:
            chapter = int(c.attrs["id"].strip("chapter"))
            if start_chapter <= chapter <= end_chapter:
                name = c.text
                address = self.base_url + c.attrs["href"]
                chapter_list.append({"name": name, "address": address, "chapter": chapter})
            else:
                if chapter > end_chapter:
                    break
        return chapter_list

    # 获取小说内容
    def get_content(self, base_object, novel_url):
        """
            获取小说内容
        :param base_object:     父类对象
        :param novel_url:       小说地址
        :return:
        """
        # 获取小说内容
        contents = base_object.wait_css_show(novel_url, "CSS_SELECTOR", "#nr1>p", None, True, 2)
        return "\n".join([c.text for c in contents][:-1])

    def get_last_new_chapter(self, base_object, novel_url):
        """
        获取最新章节
        :param base_object:
        :param novel_url:
        :return:
        """
        # 获取章节信息
        new_chapters = base_object.wait_css_show(novel_url, "ID", "add_info", "#add_info>p")[2]
        return str(new_chapters.text).strip("章节数量：").strip()